// pages/rightsMgt/customerInfo/customerInfo.js
import Common from '../../../common/common.js';
let common = new Common;
const app = getApp();
Page({ 
  data: {
    chooseId: 1,
    memberId: '', //跳转接收参数
    orderNumInfo: {}, //订单数量
    userInfo: {},
    page: 1, 
    serveTotal:0,//服务记录总数
    receiveTotal:0,//领券记录
    closingRecordTotal:0,//成交记录长度
    isEnd: false, //是否到底 
    conponsList:[],//优惠券列表
    orderList:[],//成交记录
    serveList:[],//服务记录
  },
  info:function(e){ 
    let routeUrl = '/pages/orderMgt/orderInfo/orderInfo';
    let routeType = 'navigateTo';
    let params = '?type=' + e.currentTarget.dataset.type+'&id='+e.currentTarget.dataset.id;
    common.routeLink(routeUrl, params, routeType) 
  },
  // 拨打电话
  callPhone: function(e) {
    var that = this
    wx.showModal({
      title: '您确定要拨打该电话吗？',
      content: '',
      cancelText: '取消',
      confirmText: '确定',
      success(res) {
        if (res.cancel) {//弹窗取消

        } else if (res.confirm) {//弹窗确定电话号码框弹出  
          wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phonenumber,
            success: function () {
              console.log('拨打成功')
            },
            fail: function () {
              common.showToast("拨打失败")
              console.log("拨打失败")
            }
          })
        }
      }
    }) 
  },
  // 查询会员基本信息
  queryMemberDetail: function() {
    let data = {
      memberId:this.data.memberId,
      orgSeq: getApp().globalData.shopInfo.orgSeq,
    }
    common.requestAjax('/api/member-service/v1/member/queryMemberInfoById', 'GET', data).then(res => {
      if (res.data.code == 1) {
        var time_str= res.data.result.createTime;
        var t = time_str.substr(0,10);
        this.setData({
          userInfo: res.data.result,
          createTime:t
        }) 
      }
    }).catch(err => {
      console.log(err);
    })
  },
  // 查询成交记录
  queryOrdersManager: function() {
    wx.showLoading({
      title: '正在加载中...',
    })
    let userType = app.globalData.shopInfo.userType
    let data = {
      condition: {
        memberId: this.data.memberId,
        orgSeq: app.globalData.shopInfo.orgSeq,
      },
      current: this.data.page,
      size: 10
    }
    common.requestAjax('/api/trade-service/v1/myorder/queryOrdersManager', 'POST', data).then(res => {
      if (res.data.code == 1) {
        wx.hideLoading() 
        let list = this.data.orderList || [];
        let isEnd = this.data.isEnd;
        if (res.data.records.length == 0 ) { 
          isEnd = true;//最后一行没数据 到底了
        } 
        list = list.concat(res.data.records);
        console.log(list)
          for(var i = 0;i<list.length;i++){
            console.log(list[i])
            for(var j = 0;j<list[i].memberOrderDTOS.length;j++){
              console.log(list[i].memberOrderDTOS[j])
              list[i].memberOrderDTOS[j].receivedMoney=list[i].memberOrderDTOS[j].receivedMoney.toString().replace(/-/g, "")
              list[i].memberOrderDTOS[j].totalAmount=list[i].memberOrderDTOS[j].totalAmount.toString().replace(/-/g, "")
            }
          }
        this.setData({
          orderList: list,
          isEnd,
          closingRecordTotal: res.data.total,
        }) 
        
      }else{
        common.showToast(res.data.message)
      }
    }).catch(err => {
      console.log(err);
    })
  },
  // 查询会员总订单数量
  queryOrderAmount: function() {
    let userType = app.globalData.shopInfo.userType
    let data = {
      memberId: this.data.memberId,
      orgSeq: app.globalData.shopInfo.orgSeq,
    }
    common.requestAjax('/api/trade-service/v1/myorder/queryOrderAmount', 'GET', data).then(res => {
      if (res.data.code == 1) {
        this.setData({
          orderNumInfo: res.data.result
        })
      }else{
        common.showToast(res.data.message)
      }
    }).catch(err => {
      console.log(err);
    })
  },
  // 查询领取记录
  getCouponList:function(e){
    wx.showLoading({
      title: '正在加载中...',
    })
    let userType = app.globalData.shopInfo.userType
    let data = {
      condition: {
        memberId: this.data.memberId,
        orgSeq: app.globalData.shopInfo.orgSeq, 
      },
      current: this.data.page,
      size: 10,
    }
    common.requestAjax('/api/promotion-service/v1/TPlan/getCouponList', 'POST', data).then(res => {
      if (res.data.code == 1) { 
        if (res.data.code) { 
          wx.hideLoading() 
          let list = this.data.conponsList || [];
          let isEnd = this.data.isEnd;
          if (res.data.records.length == 0) { 
            isEnd = true;//最后一行没数据 到底了
          }
          list = list.concat(res.data.records);
          this.setData({
            conponsList: list,
            isEnd,
            receiveTotal:res.data.total,
          })
        }else{
          common.showToast(res.data.message)
        }
      }
    }).catch(err => {
      console.log(err);
    })
  },
  //查询服务记录 
  queryServiceRecordListByPage:function(){
    wx.showLoading({
      title: '正在加载中...',
    })
    let data = { 
      size: 10,
      current:this.data.page,
      condition:{
        orgSeq: getApp().globalData.shopInfo.orgSeq,  
        userId: getApp().globalData.userId, 
        memberId:this.data.memberId,
      },
      ascs:[],
      descs:[],
    }
    common.requestAjax('/api/member-service/v1/memberApplyRecord/queryServiceRecordListByPage', 'POST', data).then(res => {
      if (res.data.code == 1) {
        wx.hideLoading() 
        let list = this.data.serveList || [];
        let isEnd = this.data.isEnd;
        if (res.data.records.length == 0 ) { 
          isEnd = true;//最后一行没数据 到底了
        }
        list = list.concat(res.data.records); 
        this.setData({
          serveList: list,  
          serveTotal:res.data.total,
        })  
        // 时间格式转化
        this.data.serveList.forEach((item) => {
          item.createTime =  item.createTime.substr(0,10) 
        });
        this.setData({
          serveList: list,
          isEnd,
        }) 
      }else{
        common.showToast(res.data.message)
      }
    }).catch(err => {
      console.log(err);
    })
  }, 
  onLoad: function(options) { 
    this.setData({
      memberId: options.memberId
    }) 
    this.queryMemberDetail(); //会员信息
    this.queryOrderAmount(); //订单总数
    this.queryOrdersManager(); //订单信息
    this.getCouponList();//领券记录
    this.queryServiceRecordListByPage()//服务记录
  },  
  onReachBottom: function() { 
    if (!this.data.isEnd) { 
      let page = this.data.page;
      page = page+1
      this.setData({
        page:page
      })
      if (this.data.chooseId==1){ 
        this.queryOrdersManager()//成交记录
      }else if(this.data.chooseId==2){
        this.getCouponList();//领券记录 
      }else if(this.data.chooseId==3){
        this.queryServiceRecordListByPage()//服务记录
      }
    }
  },
  // 切换tab
  chooseLit(e) { 
    let chooseId = e.currentTarget.dataset.id;
    this.setData({
      chooseId: chooseId,
      conponsList:[],//领券记录
      orderList:[],//成交记录
      serveList:[],//服务记录
      page:1,
      isEnd:false
    })
    if (this.data.chooseId==1){
      // this.queryOrderAmount(); //订单总数
      this.queryOrdersManager()//成交记录
    }else if(this.data.chooseId==2){
      this.getCouponList();//领券记录
    }else if(this.data.chooseId==3){
      this.queryServiceRecordListByPage()//服务记录
    }
  }
})